<template>
<view class="fixed" style="z-index:99999;">
  <button open-type="share" class="share">
    <view>
      <view>分享</view>
      <image class="share-icon" src="http://imgbdb3.bendibao.com/whbdb/20202/28/20200228094105_67638.png"></image>
    </view>
  </button>
  <view class="other-city" @tap="subscribe">{{text}}</view>
</view>
</template>

<script>
const app = getApp();
import util from "../../utils/util";

export default {
  data() {
    return {};
  },

  components: {},
  props: {
    action: {
      default: "",
      type: String
    },
    city: {
      default: "",
      type: String
    },
    text: {
      default: "",
      type: String
    },
    name: {
      default: "",
      type: String
    },
    tip: {
      default: "订阅消息成功",
      type: String
    }
  },
  methods: {
    // 订阅消息
    subscribe() {
      if (this.text == "已订阅") return;

      if (uni.getStorageSync("openid")) {
        const tepId = "Us5cSgjk6oFqj6UxBMaob9pUbEKTXD4D15cyINwtCPM";
        uni.requestSubscribeMessage({
          tmplIds: [tepId],
          success: res => {
            uni.getSetting({
              withSubscriptions: true,
              success: res => {},
              fail: () => {},
              complete: () => {}
            });

            if (res[tepId] === "accept") {
              let data = {
                action: "msg",
                openid: uni.getStorageSync("openid"),
                city: this.city,
                name: util.getName(this.properties.action) ? util.getName(this.properties.action) : this.name
              };
              util.request(`sub_message.php`, data, "POST").then(res => {
                uni.showToast({
                  title: this.tip,
                  icon: 'none',
                  duration: 2500
                });
                this.$emit("subsuccess"); // 统一上报函数

                util.reportMessage("dingyueliang", {
                  action: this.name
                });
              });
            }
          }
        });
      }
    }

  }
};
</script>
<style>
.fixed {
    position: fixed;
    bottom: 0;
    width: 100%;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    margin: auto;
    background: #fff;
    padding: 10rpx 0;
    z-index: 999;
}

.share {
    width: 35%;
    height: 80rpx;
    line-height: 80rpx;
    border-radius: 56rpx;
    color: #ffffff;
    font-weight: 500;
    background-color: #5395DE;
    margin-left: 20rpx;
}

.share view {
    display: flex;
    justify-content: center;
    align-items: center;
}

.share-icon {
    width: 30rpx;
    height: 30rpx;
    margin-left: 10rpx;
}

.other-city {
    width: 65%;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    border: 0;
    border-radius: 56rpx;
    margin-left: 20rpx;
    margin-right: 20rpx;
    margin-top: 10rpx;
    color: #ffffff;
    background: #5395DE;
}
</style>